﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 版权所有 (c) 2022 北京超维景生物科技有限公司 保留所有权利。
 * 
 * 创建者：huangyang
 * 电子邮箱：huangyang@tvscope.cn
 * 创建时间：2023/2/22 19:16:32
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>

using Microsoft.VisualBasic.ApplicationServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ImageK.Gui
{
    public class RotatedRectRoi: PolygonRoi
    {
        private double xstart, ystart;
        private static double DefaultRectWidth = 50;
        private double rectWidth = DefaultRectWidth;

        public RotatedRectRoi(double x1, double y1, double x2, double y2, double rectWidth): base(new float[5], new float[5], 5, FREEROI)
        {
            this.rectWidth = rectWidth;
            makeRectangle(x1, y1, x2, y2);
            state = NORMAL;
            bounds = RectangleF.Empty;
        }

        public RotatedRectRoi(int sx, int sy, ImagePlus imp):base(sx,sy,imp)
        {
            //todo:
            // super(sx, sy, imp);
            // type = FREEROI;
            // xstart = offScreenXD(sx);
            // ystart = offScreenYD(sy);
            // ImageWindow win = imp.getWindow();
            // int pixels = win != null ? (int)(win.getSize().height / win.getCanvas().getMagnification()) : imp.getHeight();
            // if (IJ.debugMode) IJ.log("RotatedRectRoi: " + (int)rectWidth + " " + pixels);
            // if (rectWidth > pixels)
            //     rectWidth = pixels / 3;
            // setDrawOffset(false);
            // bounds = null;
        }

        public override void draw(Graphics g)
        {
            base.draw(g);
            if (!overlay && ic!=null)
            {
                double mag = ic.getMagnification();
                for (int i = 0; i<4; i++)
                {
                    if (i==3) //mark starting point
                        handleColor = strokeColor!=Color.Empty ? strokeColor : ROIColor;
                    else
                        handleColor=Color.White;
                    drawHandle(g, hxs(i), hys(i));
                }
            }
        }

        private int hxs(int index)
        {
            int indexPlus1 = index<3 ? index+1 : 0;
            return xp2[index]+(xp2[indexPlus1]-xp2[index])/2;
        }

        private int hys(int index)
        {
            int indexPlus1 = index<3 ? index+1 : 0;
            return yp2[index]+(yp2[indexPlus1]-yp2[index])/2;
        }

        private double hx(int index)
        {
            int indexPlus1 = index<3 ? index+1 : 0;
            return xpf[index]+(xpf[indexPlus1]-xpf[index])/2+x;
        }

        private double hy(int index)
        {
            int indexPlus1 = index<3 ? index+1 : 0;
            return ypf[index]+(ypf[indexPlus1]-ypf[index])/2+y;
        }




        void makeRectangle(double x1, double y1, double x2, double y2)
        {
            // double length = Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
            // double angle = Math.atan((x2-x1)/(y2-y1));
            // double wsa = (rectWidth/2.0)*Math.sin((Math.PI/2.0)+angle);
            // double wca = (rectWidth/2.0)*Math.cos((Math.PI/2)+angle);
            // nPoints = 5;
            // xpf[3] = (float)(x1-wsa);
            // ypf[3] = (float)(y1-wca);
            // xpf[0] = (float)(x1+wsa);
            // ypf[0] = (float)(y1+wca);
            // xpf[1] = (float)(x2+wsa);
            // ypf[1] = (float)(y2+wca);
            // xpf[2] = (float)(x2-wsa);
            // ypf[2] = (float)(y2-wca);
            // xpf[4] = xpf[0];
            // ypf[4] = ypf[0];
            // makePolygonRelative();
            // cachedMask = null;
            // DefaultRectWidth = rectWidth;
            // showStatus();
        }




        /** Returns x1, y1, x2, y2 and width as a 5 element array. */
        public double[] getParams()
        {
            double[] paras = new double[5];
            paras[0] = hx(3);
                paras[1]  = hy(3);
                paras[2]  = hx(1);
                paras[3]  = hy(1);
                paras[4]  = rectWidth;
            return paras;
        }

        /** Always returns true. */
        public bool subPixelResolution()
        {
            return true;
        }
    }
}
